//
//  Samurai.m
//  SpaceViking
//
//  Created by gnt on 12/8/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Samurai.h"


@implementation Samurai

@synthesize runAnim;
@synthesize runLeftAnim;
@synthesize runRightAnim;
@synthesize turnSamurai;
@synthesize deadTroop;

- (void) dealloc {
    [runAnim release];
    [runLeftAnim release];
    [runRightAnim release];
    [super dealloc];
}

-(BOOL)isCarryingWeapon {
    return isCarryingMallet;
}

-(int)getWeaponDamage {
    if (isCarryingMallet) {
        return kSamuraiMalletDamage;
    }
    return kSamuraiFistDamage;
}


-(void)checkAndClampSpritePosition {
}

#pragma mark -
-(void)changeState:(CharacterStates)newState {
    [self stopAllActions];
    id action = nil;
//    id movementAction = nil;
//    CGPoint newPosition;
    [self setCharacterState:newState];
    
    switch (newState) {
        case kStateIdle:
            break;
            
        case kStateRunCenter:
        {
//            action = [[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:runAnim restoreOriginalFrame:NO]]retain];
            action = [[CCRepeatForever actionWithAction:[CCAnimate actionWithDuration:0.3f 
                                                                            animation:runAnim 
                                                                 restoreOriginalFrame:NO]]retain];
            //newState = kStateIdle;
        }            
            break;
            
            
        case kStateRunLeft:
        {
            action = [[CCRepeatForever actionWithAction:[CCAnimate actionWithDuration:0.3f 
                                                                            animation:runLeftAnim 
                                                                 restoreOriginalFrame:NO]]retain];
        }
            break;
            
        case kStateRunRight:
        {
            action = [[CCRepeatForever actionWithAction:[CCAnimate actionWithDuration:0.3f 
                                                                            animation:runRightAnim 
                                                                 restoreOriginalFrame:NO]]retain];
        }
            break;
            
  
        default:
            break;
    }
    if (action != nil) {
        [self runAction:action];
    }
}

#pragma mark -
-(void)updateStateWithDeltaTime:(ccTime)deltaTime {
    if (self.characterState == kStateDead) 
        return; // Nothing to do if the Samurai is dead

//    CGRect myBoundingBox = [self adjustedBoundingBox];
//    for (GameCharacter *character in listOfGameObjects) {
//        // This is Ole the Viking himself
//        // No need to check collision with one's self
//        if ([character tag] == kSamuraiSpriteTagValue) 
//            continue;
//        
//        CGRect characterBox = [character adjustedBoundingBox];
//        if (CGRectIntersectsRect(myBoundingBox, characterBox)) {
//
//        }
//    }    
    
    [self checkAndClampSpritePosition];

    BackgroundLayer* bg = [BackgroundLayer sharedBackgroundLayer];
    if(bg.turn != turnSamurai) {
        if(bg.turn == LEFT) {
            [self changeState:kStateRunRight];
        }
        else if(bg.turn == RIGHT) {
            [self changeState:kStateRunLeft];
        }
        else if(bg.turn == STRIGHT) {
            [self changeState:kStateRunCenter];
        }
        turnSamurai = bg.turn;
    }
}

#pragma mark -
-(CGRect)adjustedBoundingBox {
    // Adjust the bouding box to the size of the sprite 
    // without the transparent space
    CGRect samuraiBoundingBox = [self boundingBox];
    float xOffset;
    float xCropAmount = samuraiBoundingBox.size.width * 0.5482f;
    float yCropAmount = samuraiBoundingBox.size.height * 0.095f;
    
    if ([self flipX] == NO) {
        // Viking is facing to the rigth, back is on the left
        xOffset = samuraiBoundingBox.size.width * 0.1566f;
    } else {
        // Viking is facing to the left; back is facing right
        xOffset = samuraiBoundingBox.size.width * 0.4217f;
    }
    samuraiBoundingBox = 
    CGRectMake(samuraiBoundingBox.origin.x + xOffset, 
               samuraiBoundingBox.origin.y, 
               samuraiBoundingBox.size.width - xCropAmount, 
               samuraiBoundingBox.size.height - yCropAmount);

    
    return samuraiBoundingBox;
}

#pragma mark -
-(void)initAnimations {
    [self setRunAnim:[self loadPlistForAnimationWithName:@"runAnim" 
                                            andClassName:NSStringFromClass([self class])]];
    [self setRunLeftAnim:[self loadPlistForAnimationWithName:@"runLeftAnim" 
                                                andClassName:NSStringFromClass([self class])]];
    [self setRunRightAnim:[self loadPlistForAnimationWithName:@"runRightAnim" 
                                                 andClassName:NSStringFromClass([self class])]];
}

-(id) init {
    if( (self=[super init]) ) {
        self.gameObjectType = kVikingType;
        myLastPunch = kRightHook;
//        millisecondsStayingIdle = 0.0f;
        isCarryingMallet = NO;
        self.characterState = kStateRunCenter;
        [self initAnimations];
        turnSamurai = STRIGHT;
        [self changeState:self.characterState];
        [self setDeadTroop:0];
//        deadTroop = 0;
    }
    return self;
}

@end
